package com.allen.study.domain.service;

import com.allen.study.common.base.DomainResponse;
import com.allen.study.domain.entity.EmployeeInfo;
import com.allen.study.domain.entity.SysEmpRole;
import com.allen.study.domain.repository.IEmployeeInfoRepo;
import com.allen.study.domain.repository.ISysEmpRoleRepo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户信息表领域服务
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class EmployeeInfoService {

    /**
     * 用户信息表资源库
     */
    private final IEmployeeInfoRepo employeeInfoRepo;
    private final ISysEmpRoleRepo sysEmpRoleRepo;

    /**
     * 创建用户信息表
     *
     * @param employeeInfo 用户信息表实体
     * @return 响应结果
     */
    public DomainResponse<Void> create(@NotNull EmployeeInfo employeeInfo) {
        // 获取当前登录用户
        // SecurityUser securityUser = SecurityUserHelper.get();
        // 设置租户
        // employeeInfo.setTenantId(securityUser.getTenantId());

        // 保存用户信息表
        employeeInfoRepo.create(employeeInfo);
        return DomainResponse.ok();
    }

    /**
     * 根据主键删除用户信息表
     *
     * @param employeeInfoId 用户信息表主键
     * @return 响应结果
     */
    public DomainResponse<Void> deleteById(@NotNull String employeeInfoId) {
        // 根据主键查询用户信息表
        EmployeeInfo employeeInfo = employeeInfoRepo.queryById(employeeInfoId);
        // 用户信息表数据存在且租户正确，删除用户信息表数据
        if (ObjectUtils.isNotEmpty(employeeInfo) && employeeInfo.checkTenant()) {
            employeeInfoRepo.deleteById(employeeInfoId);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键更新用户信息表
     *
     * @param employeeInfo 用户信息表实体
     * @return 响应结果
     */
    public DomainResponse<Void> updateById(@NotNull EmployeeInfo employeeInfo) {
        // 根据主键查询用户信息表
        EmployeeInfo exists = employeeInfoRepo.queryById(employeeInfo.getId());
        // 用户信息表数据存在且租户正确，更新用户信息表数据
        if (ObjectUtils.isNotEmpty(exists) && exists.checkTenant()) {
            employeeInfoRepo.updateById(employeeInfo);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键查询用户信息表
     *
     * @param employeeInfoId 用户信息表主键
     * @return 响应结果
     */
    public DomainResponse<EmployeeInfo> queryById(@NotNull String employeeInfoId) {
        EmployeeInfo employeeInfo = employeeInfoRepo.queryById(employeeInfoId);
        return DomainResponse.ok(employeeInfo);
    }

    public DomainResponse<List<EmployeeInfo>> queryEmpsByRoleKey(@NotNull String roleKey) {
        List<SysEmpRole> sysEmpRoles = sysEmpRoleRepo.queryByRoleKey(roleKey);
        List<String> empIdList =
                Optional.ofNullable(sysEmpRoles).orElse(Collections.emptyList()).stream().map(SysEmpRole::getEmployeeId).collect(Collectors.toList());
        List<EmployeeInfo> employeeInfos = employeeInfoRepo.queryByEmpIds(empIdList);
        return DomainResponse.ok(employeeInfos);
    }
}
